import {defineStore} from "pinia";
import {ref} from 'vue'
import {
    dictionaryCreate,
    dictionaryDelete,
    dictionaryDeleteBatch,
    dictionaryList,
    dictionaryUpdate,
    dictionaryUpdateStatus
} from "@/services/api/dictionary.ts";
import useMessage from "@/utils/useMessage.ts";
import {Modal} from "ant-design-vue";
import {hasPermission} from "@/utils/permission.ts";

export const useDictionaryStore = defineStore('dictionary',()=>{
    const dataSource = ref<DictionaryType[]>([])
    const loading = ref<boolean>(false)
    const data = ref<DictionaryType[]>([])
    const getList = async (force?:boolean) => {
        if (!force && dataSource.value.length > 0) return
        try {
            loading.value = true
            const res = await dictionaryList()
            if (res.code === 200){
                dataSource.value = res.data
            }
        } catch (e:any) {
            useMessage(500, e.message || '请求失败，请重试或是联系我们')
        }finally {
            loading.value = false
        }
    }
    const getDictionary = async (type:string) => {
        loading.value = true
        data.value =  dataSource.value.filter(item => item.type === type)
        setTimeout(()=>loading.value = false,100)
    }
    const create = async (form:DictionaryFormType) => {
        if (!hasPermission('dictionary:create')) return false
        try {
            const res = await dictionaryCreate(form)
            useMessage(res.code, res.message)
            if (res.code === 200) {
                await getList(true)
                await getDictionary(form.type)
                return true
            }
            return false
        } catch (e:any) {
            useMessage(500, e.message || '创建字典失败')
            return false
        }
    }
    const update = async (form:DictionaryFormType) => {
        if (!hasPermission('dictionary:update')) return false
        try {
            const res = await dictionaryUpdate(form?.id as number,form)
            useMessage(res.code, res.message)
            if (res.code === 200) {
                const items = dataSource.value.find(item => item.id === form?.id)
                if (items) {
                    Object.assign(items,form)
                }
                return true
            }
            return false
        } catch (e:any) {
            useMessage(500, e.message || '更新字典失败')
        }
    }
    //批量更新状态
    const batchUpdate = async (id:number[],form:{status:number}) => {
        if (!hasPermission('dictionary:update')) return false
        try {
            const res = await dictionaryUpdateStatus(id,form)
            useMessage(res.code, res.message)
            if (res.code === 200) {
                for (const item of dataSource.value) {
                    if (id.includes(item.id)) {
                        Object.assign(item, form)
                    }
                }
            }
        } catch (e:any) {
            useMessage(500, e.message || '更新字典失败')
        }
    }
    const remove = async (id:number) => {
        if (!hasPermission('dictionary:delete')) return false
        try {
            Modal.confirm({
                title: '删除确认',
                content: '确定要删除此字典吗？此操作不可逆，请谨慎操作！',
                onOk: async () => {
                    const res = await dictionaryDelete(id)
                    useMessage(res.code, res.message)
                    if (res.code === 200) {
                        dataSource.value = dataSource.value.filter(item => item.id !== id)
                        data.value = data.value.filter(item => item.id !== id)
                    }
                }
            })
        } catch (e:any) {
            useMessage(500, e.message || '删除字典失败')
        }
    }
    //批量删除
    const batchRemove = async (id:number[]) => {
        try {
            Modal.confirm({
                title: '删除确认',
                content: '确定要删除选中的字典吗？此操作不可逆，请谨慎操作！',
                onOk: async () => {
                    const res = await dictionaryDeleteBatch(id)
                    useMessage(res.code, res.message)
                    if (res.code === 200) {
                        dataSource.value = dataSource.value.filter(item => !id.includes(item.id))
                        data.value = data.value.filter(item => !id.includes(item.id))
                    }
                }
            })
        } catch (e:any) {
            useMessage(500, e.message || '删除字典失败')
        }
    }
    return {
        dataSource,
        loading,
        data,
        getList,
        getDictionary,
        create,
        update,
        batchUpdate,
        remove,
        batchRemove
    }
},{persist: {pick:['dataSource']}})